package com.editso.haier.entity;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户信息表
 * */
@Entity
@Table
@Getter
@Setter
@NoArgsConstructor
public class User {

    @Id
    @Column
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;

    @Column(unique = true, nullable = false)
    private String name;

    @Column
    private String password;

    @Column
    private String sex;

    @Column
    private String phone;

    @Column
    private String email;

    @Column(columnDefinition = "datetime default now()",
            insertable = false,
            updatable = false)
    private Timestamp createData;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable( name = "vip_user",
                inverseJoinColumns = @JoinColumn(name = "vip_id"))
    private Set<Vip> vips = new HashSet<>();

    /**
     * 所有我发送的消息, 包括评论信息
     * */
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private Set<Message> messages = new HashSet<>();

    /**
     * 该用户在此担任的角色
     * */
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "user_role",
                inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles = new HashSet<>();

    /**
     * 我的所有需要购买的商品
     * */
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private Set<ShoppingCar> shoppingCars = new HashSet<>();


    public User(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                ", createData=" + createData +
                '}';
    }
}
